package com.meituan.catering.management.order.biz.model.converter;

import com.meituan.catering.management.common.model.biz.UserContextBO;
import com.meituan.catering.management.order.api.http.model.enumeration.CateringOrderStatusEnum;
import com.meituan.catering.management.order.biz.model.request.BillCateringOrderBizRequest;
import com.meituan.catering.management.order.biz.model.request.PlaceCateringOrderBizRequest;
import com.meituan.catering.management.order.dao.model.CateringOrderDO;
import com.meituan.catering.management.order.remote.model.response.ShopDetailRemoteResponse;

import java.util.Date;

/**
 * 从其他数据模型向订单DO的转换器
 */
public class CateringOrderDOConverter {
    /**
     * 将门店信息、请求中的信息、用户信息转换为CateringOrderDO对象
     *
     * @param shopDetailRemoteResponse     RPC门店信息
     * @param placeCateringOrderBizRequest 下单请求
     * @param userContextBO                用户信息
     * @return
     */
    public static CateringOrderDO toCateringOrderDO(ShopDetailRemoteResponse shopDetailRemoteResponse,
                                                    PlaceCateringOrderBizRequest placeCateringOrderBizRequest,
                                                    UserContextBO userContextBO) {
        CateringOrderDO cateringOrderDO = new CateringOrderDO();
        cateringOrderDO.setStatus(CateringOrderStatusEnum.PLACED);
        cateringOrderDO.setShopId(shopDetailRemoteResponse.getId());
        cateringOrderDO.setShopBusinessNo(placeCateringOrderBizRequest.getShopBusinessNo());
        cateringOrderDO.setShopNameOnPlace(shopDetailRemoteResponse.getName());
        cateringOrderDO.setTableNo(placeCateringOrderBizRequest.getTableNo());
        cateringOrderDO.setCustomerCount(placeCateringOrderBizRequest.getCustomerCount());
        cateringOrderDO.setTotalPrice(placeCateringOrderBizRequest.getTotalPrice());
        cateringOrderDO.setComment(placeCateringOrderBizRequest.getComment());
        cateringOrderDO.setTenantId(userContextBO.getTenantId());
        cateringOrderDO.setCreatedBy(userContextBO.getUserId());
        cateringOrderDO.setCreatedAt(new Date());
        cateringOrderDO.setLastModifiedBy(userContextBO.getUserId());
        cateringOrderDO.setLastModifiedAt(new Date());
        return cateringOrderDO;
    }

    /**
     * 制作：更改一部分DO对象参数
     *
     * @param cateringOrderDO DO对象
     * @param version         乐观锁版本号
     * @return
     */
    public static CateringOrderDO toPrepareCateringOrderDO(CateringOrderDO cateringOrderDO, Integer version) {
        cateringOrderDO.setStatus(CateringOrderStatusEnum.PREPARING);
        cateringOrderDO.setVersion(version);
        cateringOrderDO.setLastModifiedAt(new Date());
        return cateringOrderDO;
    }

    /**
     * 出餐：更改一部分DO对象参数
     *
     * @param cateringOrderDO DO对象
     * @param version         乐观锁版本号
     * @return
     */
    public static CateringOrderDO toProduceCateringOrderDO(CateringOrderDO cateringOrderDO, Integer version) {
        cateringOrderDO.setStatus(CateringOrderStatusEnum.PREPARED);
        cateringOrderDO.setVersion(version);
        cateringOrderDO.setLastModifiedAt(new Date());
        return cateringOrderDO;
    }

    /**
     * 结账：更改一部分DO对象参数
     *
     * @param cateringOrderDO             DO对象
     * @param billCateringOrderBizRequest 结账请求
     * @return
     */
    public static CateringOrderDO toCateringOrderDO(CateringOrderDO cateringOrderDO, BillCateringOrderBizRequest billCateringOrderBizRequest) {
        cateringOrderDO.setStatus(CateringOrderStatusEnum.BILLED);
        cateringOrderDO.setVersion(billCateringOrderBizRequest.getVersion());
        cateringOrderDO.setBillingPromotion(billCateringOrderBizRequest.getPromotion());
        cateringOrderDO.setBillingPaid(cateringOrderDO.getTotalPrice().subtract(billCateringOrderBizRequest.getPromotion()));
        cateringOrderDO.setBillingPaymentChannel(billCateringOrderBizRequest.getPaymentChannel());
        cateringOrderDO.setLastModifiedAt(new Date());
        return cateringOrderDO;
    }
}
